package com.cn.onetrip.adapter;

import java.util.ArrayList;
import java.util.List;

import com.cn.onetrip.objects.InfosListObj;
import com.cn.onetrip.objects.TipsType;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.activity.R;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class InfoGridAdapter extends BaseAdapter {

	private List<InfosListObj> tipsList = new ArrayList<InfosListObj>();
	private LayoutInflater mInflater;
	private Context mContext;

	public InfoGridAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(context);

		tipsList = SysApplication.infosMapList.get("tips");
		if (tipsList.get(tipsList.size() - 1).tip_type_cd != TipsType.travels) {
			InfosListObj objTravel = new InfosListObj();
			objTravel.tip_type_cd = TipsType.travels;
			objTravel.title = context.getResources().getString(
					R.string.info_travels);
			tipsList.add(objTravel);
		}
	}

	public int getCount() {
		if (tipsList.size() % 2 == 1) {
			return (tipsList.size() + 1);
		}
		return tipsList.size();
	}

	public Object getItem(int position) {
		int count = getCount() - 1;
		if (position >= count) {
			return null;
		}
		return tipsList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ItemViewTag viewTag;
		InfosListObj model = null;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.grid_item_info, null);

			// construct an item tag
			viewTag = new ItemViewTag(
					(RelativeLayout) convertView.findViewById(R.id.grid_id),
					(ImageView) convertView.findViewById(R.id.grid_icon),
					(TextView) convertView.findViewById(R.id.grid_name));
			convertView.setTag(viewTag);

			if (tipsList.size() % 2 == 1 && position == tipsList.size()) {
				model = tipsList.get(0);
				if (tipsList.size() % 4 == 3) {
					viewTag.gridLayout.setBackgroundColor(mContext
							.getResources().getColor(
									R.color.info_itemblue_color));
				} else {
					viewTag.gridLayout.setBackgroundColor(mContext
							.getResources().getColor(
									R.color.info_itemwhite_color));
				}
				viewTag.mIcon.setBackgroundResource(getDrawableID(model, 1));
				viewTag.mIcon.setVisibility(View.INVISIBLE);
				return convertView;
			}
		} else {
			viewTag = (ItemViewTag) convertView.getTag();
		}
		model = tipsList.get(position);
		if (position % 4 == 0 || (position + 1) % 4 == 0) {
			viewTag.gridLayout.setBackgroundColor(mContext.getResources()
					.getColor(R.color.info_itemblue_color));
			// set icon
			viewTag.mIcon.setBackgroundResource(getDrawableID(model, 1));
			if (position == (getCount() - 1) && tipsList.size() % 2 == 1) {
				viewTag.mIcon.setBackgroundResource(getDrawableID(model, 1));
			}
		} else {
			// set icon
			viewTag.mIcon.setBackgroundResource(getDrawableID(model, 2));

			viewTag.gridLayout.setBackgroundColor(mContext.getResources()
					.getColor(R.color.info_itemwhite_color));
		}

		return convertView;
	}

	class ItemViewTag {
		protected RelativeLayout gridLayout;
		protected ImageView mIcon;
		protected TextView mName;

		/**
		 * The constructor to construct a navigation view tag
		 * 
		 * @param name
		 *            the name view of the item
		 * @param size
		 *            the size view of the item
		 * @param icon
		 *            the icon view of the item
		 */
		public ItemViewTag(RelativeLayout gridLayout, ImageView icon,
				TextView name) {
			this.gridLayout = gridLayout;
			this.mName = name;
			this.mIcon = icon;
		}
	}

	private int getDrawableID(InfosListObj model, int which) {
		int drawableID = 0;
		switch (model.tip_type_cd) {
		case TipsType.tickets:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_tickets" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.traffic:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_traffic" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.todo:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_todo" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.consume:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_consume" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.arm:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_arm" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.feast:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_feast" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.way:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_way" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.alert:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_alert" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;
		case TipsType.tips:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_tips" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;

		case TipsType.travels:
			drawableID = mContext.getResources().getIdentifier(
					"info_selector_travel" + which, "drawable",
					mContext.getApplicationContext().getPackageName());
			break;

		default:
			break;
		}
		return drawableID;
	}

}